const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const app = express();

// 引入users.js
const users = require("./routes/api/user");

// db config
const db = require("./config/key").mongoURI;




// 使用body-parser中间件
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

// 使用passport
app.use(passport.initialize());
// 配置passport
require("./config/passport")(passport);

//connect
mongoose.connect(db, { useNewUrlParser: true })
    .then(() => console.log("mongodb connected"))
    .catch(err => console.log(err))




// app.get("/",(req,res)=>{
//   res.send("hello world");
// })


// 使用routes
app.use("/api/users",users);

const port = process.env.PORT || 5000;

app.listen(port,()=> {
  console.log(`server running on port ${port}`);
})